package com.ykq.controller;

import com.github.pagehelper.PageInfo;
import com.ykq.entity.Staff;
import com.ykq.service.StaffService;
import com.ykq.vo.Result;
import com.ykq.vo.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @program: keyboard
 * @description:
 * @author: 闫克起2
 * @create: 2023-07-21 14:24
 **/
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @GetMapping("info")
    public Result info(HttpSession session) {
        //从session会话中获取当前用户信息
        Object staff = session.getAttribute("staff");
        if (staff == null) {
            return new Result(500, "请先登录");
        }
        return new Result(200, "获取用户信息成功", staff);
    }

    @GetMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("staff");
        return new Result(200, "用户退出成功");
    }

    @PostMapping("list")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       @RequestBody StaffVo staffVo,
                       HttpSession session
                       ){
        //从session会话中获取当前用户信息
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchId = staffVo.getBranchId();
        if(branchId==null){ //如果没有选择分店id--则查询的为当前用户所在的分店id
            staffVo.setBranchId(staff.getBranchid());
        }
        PageInfo<Staff> pageInfo = staffService.findAll(currentPage, pageSize, staffVo);
        return new Result(200,"查询成功",pageInfo);
    }
}








